package com.uenpay.im.ui.old.activity.company;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qyx.android.weight.view.MyListView;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.base.BaseActivity;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-4-1 下午5:31:21
 * @copyRight http://www.qiyunxin.com
 * @function 选择部门成员
 */
public class ChooseEmployeeActivity extends BaseActivity {

	private MyListView employee_listview, department_listview;
	private DepartmentsAdapter mDepartmentsAdapter = null;
	private ChooseEmployeeAdapter mEmployeeAdapter = null;
	private CompanyHandle mCompanyHandle = new CompanyHandle();
	private DepartmentEntity departmentEntity = null;
	private View loading;
	/**
	 * 是否为单选
	 */
	private boolean isRadio = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IMApplication.getInstance().addActivity(this);
		setContentView(R.layout.subsectors_layout);
		departmentEntity = (DepartmentEntity) getIntent().getSerializableExtra(
				"departmentEntity");
		isRadio = getIntent().getBooleanExtra("isRadio", false);
		initView();
		initListener();
		loading.setVisibility(View.VISIBLE);
		getdata();
		getCustByDepart();
	}

	/**
	 * 获取该部门下的子部门
	 */
	private void getdata() {
		mCompanyHandle.getDepartByPidList(departmentEntity.departid,
				departmentEntity.entid, new CompanyHandle.IGetDepartListResult() {

					@Override
					public void onGetDepartResult(int status, String msg,
							ArrayList<DepartmentEntity> arrayList) {
						loading.setVisibility(View.GONE);
						if (status == HttpInvokeResultCode.RESULT_OK) {
							if (arrayList != null && arrayList.size() > 0) {
								mDepartmentsAdapter = new DepartmentsAdapter(
										ChooseEmployeeActivity.this, arrayList,
										false);
								department_listview
										.setAdapter(mDepartmentsAdapter);
							}
						}
					}
				});
	}

	/**
	 * 获取该部门下成员
	 */
	private void getCustByDepart() {
		mCompanyHandle.getCustbyDepart(departmentEntity.departid,
				departmentEntity.entid, new CompanyHandle.IGetCustByDepartListener() {

					@Override
					public void onGetCustByDepartResult(int status, String msg,
							ArrayList<EmployeeEntity> arrayList) {
						loading.setVisibility(View.GONE);
						if (arrayList != null && arrayList.size() > 0) {
							mEmployeeAdapter = new ChooseEmployeeAdapter(
									ChooseEmployeeActivity.this, isRadio,
									arrayList);
							employee_listview.setAdapter(mEmployeeAdapter);
						}
					}
				});
	}

	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}

	@Override
	protected void initView() {
		((TextView) findViewById(R.id.title_right_tv)).setText(R.string.sure);
		findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
		loading = findViewById(R.id.loading);
		TextView title = (TextView) findViewById(R.id.title_textview);
		employee_listview = (MyListView) findViewById(R.id.employee_listview);
		department_listview = (MyListView) findViewById(R.id.department_listview);
		if (departmentEntity != null) {
			title.setText(departmentEntity.departname);
		}
	}

	@Override
	protected void initListener() {
		findViewById(R.id.back_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						finish();
					}
				});
		findViewById(R.id.title_right_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						if (mEmployeeAdapter == null)
							return;
						ArrayList<EmployeeEntity> arraylist = mEmployeeAdapter
								.getSelectedUser();
						if (arraylist != null) {
							Intent intent = new Intent();
							intent.putExtra("arraylist", arraylist);
							setResult(RESULT_OK, intent);
							finish();
						}
					}
				});
		department_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				DepartmentEntity mDepartmentEntity = (DepartmentEntity) arg0
						.getAdapter().getItem(arg2);
				if (mDepartmentEntity != null) {
					Bundle bundle = new Bundle();
					bundle.putSerializable("departmentEntity",
							mDepartmentEntity);
					bundle.putInt("departid", 0);
					ActivityHandle.startActivity(ChooseEmployeeActivity.this,
							ChooseEmployeeActivity.class, bundle, 101);
				}
			}

		});

		employee_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				EmployeeEntity mEmployeeEntity = (EmployeeEntity) arg0
						.getAdapter().getItem(arg2);
				if (mEmployeeEntity != null) {
					mEmployeeAdapter.setSelected(mEmployeeEntity.custid);
				}
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 101 && resultCode == RESULT_OK) {
			ArrayList<EmployeeEntity> arraylist = (ArrayList<EmployeeEntity>) data
					.getSerializableExtra("arraylist");
			Intent intent = new Intent();
			intent.putExtra("arraylist", arraylist);
			setResult(RESULT_OK, intent);
			finish();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
